package com.hos.db.create;

import com.hos.db.create.art.ArticleTable;
import com.hos.db.create.consult.ConsultChatTable;
import com.hos.db.create.consult.ConsultTable;
import com.hos.db.create.ill.IllBuildTable;
import com.hos.db.create.ill.IllCureTable;
import com.hos.db.create.ill.IllDataTable;
import com.hos.db.create.ill.IllHistorysTable;
import com.hos.db.create.ill.IllOptionTable;
import com.hos.db.create.ill.IllResultTable;
import com.hos.db.create.ill.IllsTable;
import com.hos.db.create.msg.MsgTable;
import com.hos.db.create.relation.RelationTable;
import com.hos.db.create.sms.SmsTable;
import com.hos.db.create.user.UserDocTable;
import com.hos.db.create.user.UserPatTable;
import com.hos.db.create.user.UserSysTable;
import com.hos.db.create.visit.VisitTable;
import com.hos.utile.DLog;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by guom on 2018/10/15.
 */
public class CreateManager {
    private JdbcTemplate jdbcTemplate;

    public CreateManager(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    //创建表
    public void caretePatTable() {
        test();
        testIll();
    }


    //插入选择数据
    private void testOption(final List<String[]> datas) {

        String sql = "INSERT INTO " + IllOptionTable.tableName
                + "("
                + IllOptionTable.tableCategoryId + ","
                + IllOptionTable.tableCategoryName + ","
                + IllOptionTable.tableCategoryValue
                + ")"
                + " VALUES (?,?,?)";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                String[] bean = datas.get(i);
                preparedStatement.setString(1, bean[0]);
                preparedStatement.setString(2, bean[1]);
                preparedStatement.setString(3, bean[2]);
            }

            @Override
            public int getBatchSize() {
                return datas.size();
            }
        });

    }

    private void test() {
        String sql = UserPatTable.careteTable;
        DLog.e("建患者表 sql:" + sql);
        jdbcTemplate.execute(sql);
        //

        sql = UserDocTable.careteTable;
        DLog.e("建医生表 sql:" + sql);
        jdbcTemplate.execute(sql);
        //

        sql = UserSysTable.careteTable;
        DLog.e("建系统表 sql:" + sql);
        jdbcTemplate.execute(sql);
        //

        sql = ArticleTable.careteTable;
        DLog.e("建文章表 sql:" + sql);
        jdbcTemplate.execute(sql);
        //
        sql = SmsTable.careteTable;
        DLog.e("建短信表 sql:" + sql);
        jdbcTemplate.execute(sql);
        //
        sql = VisitTable.careteTable;
        DLog.e("随访表 sql:" + sql);
        jdbcTemplate.execute(sql);

        sql = MsgTable.careteTable;
        DLog.e("系统消息 sql:" + sql);
        jdbcTemplate.execute(sql);

        sql = ConsultTable.careteTable;
        DLog.e("咨询表 sql:" + sql);
        jdbcTemplate.execute(sql);

        sql = ConsultChatTable.careteTable;
        DLog.e("咨询聊天表 sql:" + sql);
        jdbcTemplate.execute(sql);

        sql = RelationTable.careteTable;
        DLog.e("关系表 sql:" + sql);
        jdbcTemplate.execute(sql);
        //
    }

    //病历方面的表
    private void testIll() {
        String sql = IllsTable.careteTable;
        DLog.e("建病历 sql:" + sql);
        jdbcTemplate.execute(sql);
        //
        sql = IllBuildTable.careteTable;
        DLog.e("建体格 sql:" + sql);
        jdbcTemplate.execute(sql);
        //
        sql = IllCureTable.careteTable;
        DLog.e("建病情治疗 sql:" + sql);
        jdbcTemplate.execute(sql);
        //
        sql = IllDataTable.careteTable;
        DLog.e("建影像资料 sql:" + sql);
        jdbcTemplate.execute(sql);
        //
        sql = IllHistorysTable.careteTable;
        DLog.e("建病史 sql:" + sql);
        jdbcTemplate.execute(sql);
        //
        sql = IllResultTable.careteTable;
        DLog.e("建病情治疗结果/出院情况 sql:" + sql);
        jdbcTemplate.execute(sql);
        //
        sql = IllOptionTable.careteTable;
        DLog.e("建病情填写选择 sql:" + sql);
        jdbcTemplate.execute(sql);
        //
        testOption(IllOptionTable.getOptions());
        DLog.e("插入病情填写选择数据");

    }
}
